package com.zscat.mallplus.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.exception.BusinessMallException;
import com.zscat.mallplus.pms.entity.PmsProductAttribute;
import com.zscat.mallplus.pms.entity.PmsProductAttributeCategory;
import com.zscat.mallplus.pms.mapper.PmsProductAttributeCategoryMapper;
import com.zscat.mallplus.pms.mapper.PmsProductAttributeMapper;
import com.zscat.mallplus.pms.service.IPmsProductAttributeCategoryService;
import com.zscat.mallplus.pms.service.IPmsProductAttributeService;
import com.zscat.mallplus.pms.vo.ProductAttrInfo;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品属性参数表 服务实现类
 * </p>
 *
 * @author zscat
 * @since 2019-04-19
 */
@Slf4j
@Service
public class PmsProductAttributeServiceImpl extends ServiceImpl<PmsProductAttributeMapper, PmsProductAttribute> implements IPmsProductAttributeService {

    @Resource
    private PmsProductAttributeMapper productAttributeMapper;
    @Resource
    private IPmsProductAttributeCategoryService productAttributeCategoryService;

    @Override
    public List<ProductAttrInfo> getProductAttrInfo(Long productCategoryId) {
        return productAttributeMapper.getProductAttrInfo(productCategoryId);
    }

    @Transactional
    @Override
    public boolean saveAndUpdate(PmsProductAttribute entity) {
        int count = productAttributeMapper.selectCount(new QueryWrapper<PmsProductAttribute>().eq("product_attribute_category_id", entity.getProductAttributeCategoryId()).eq("type", entity.getType()));
        if (count >= 3) {
            throw new BusinessMallException("规格或者属性数量不能超过3个");
        }
        productAttributeMapper.insert(entity);
        //新增商品属性以后需要更新商品属性分类数量

        PmsProductAttributeCategory pmsProductAttributeCategory = productAttributeCategoryService.getById(entity.getProductAttributeCategoryId());
        if (entity.getType() == 0) {
            pmsProductAttributeCategory.setAttributeCount(pmsProductAttributeCategory.getAttributeCount() + 1);
        } else if (entity.getType() == 1) {
            pmsProductAttributeCategory.setParamCount(pmsProductAttributeCategory.getParamCount() + 1);
        }
        productAttributeCategoryService.updateById(pmsProductAttributeCategory);
        return true;
    }

    /**
     * 删除规格或参数
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object delete(Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("商品属性参数表id");
            }
            // 根据商品属性ID查询商品属性详情
            PmsProductAttribute productAttribute = productAttributeMapper.selectById(id);
            // 根据属性分类ID查询属性分类详情
            PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getById(productAttribute.getProductAttributeCategoryId());
            if (productAttributeCategory != null) {
            	if (productAttribute.getType() == 0) { // 规格
            		productAttributeCategory.setAttributeCount(productAttributeCategory.getAttributeCount() - 1);
            	} else {
            		productAttributeCategory.setParamCount(productAttributeCategory.getParamCount() - 1);
            	}
            	productAttributeCategoryService.updateById(productAttributeCategory);
			}
            productAttributeMapper.deleteById(id);
            return new CommonResult().success("删除成功");
        } catch (Exception e) {
            log.error("删除商品属性参数表：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
    }
}
